package com.example.mylibrary.util;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;

import com.example.mylibrary.MyApp;

import java.util.Set;

/**
 * Created by fairplay on 2018/6/8.
 */

public class PreferencesUtils {
    private static final String PREFERENCE_NAME = "preferences_file";
    private static Context mContext = MyApp.getInstance().getContext();
    private static SharedPreferences settings;
    private static SharedPreferences.Editor editor;

    private PreferencesUtils() {
    }

    public static boolean putString(String key, String value) {
        editor.putString(key, value);
        return editor.commit();
    }

    public static String getString(String key) {
        return getString(key, (String)null);
    }

    public static String getString(String key, String defaultValue) {
        return settings.getString(key, defaultValue);
    }

    public static boolean putInt(String key, int value) {
        editor.putInt(key, value);
        return editor.commit();
    }

    public static int getInt(String key) {
        return getInt(key, -1);
    }

    public static int getInt(String key, int defaultValue) {
        return settings.getInt(key, defaultValue);
    }

    public static boolean putLong(String key, long value) {
        editor.putLong(key, value);
        return editor.commit();
    }

    public static long getLong(String key) {
        return getLong(key, -1L);
    }

    public static long getLong(String key, long defaultValue) {
        return settings.getLong(key, defaultValue);
    }

    public static boolean putFloat(String key, float value) {
        editor.putFloat(key, value);
        return editor.commit();
    }

    public static float getFloat(String key) {
        return getFloat(key, -1.0F);
    }

    public static float getFloat(String key, float defaultValue) {
        return settings.getFloat(key, defaultValue);
    }

    public static boolean putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        return editor.commit();
    }

    @TargetApi(11)
    public static boolean putStringSet(String key, Set<String> value) {
        editor.putStringSet(key, value);
        return editor.commit();
    }

    @TargetApi(11)
    public static Set<String> getStringSet(String key) {
        return settings.getStringSet(key, (Set)null);
    }

    public static boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        return settings.getBoolean(key, defaultValue);
    }

    static {
        settings = mContext.getSharedPreferences("preferences_file", 0);
        editor = settings.edit();
    }
}
